package cn.gz.demo.ui;

import android.os.Bundle;

import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;

import cn.gz.demo.R;
import cn.gz.demo.adapter.FragmentTabAdapter;
import cn.gz.demo.databinding.Tab1LayoutBinding;
import cn.gz.demo.ui.base.BaseActivity;
import cn.gz.demo.ui.tab_fragment.TestFrament;

//import androidx.viewpager.widget.ViewPager;

/**
 * Created by Lyz on 2017/3/8.
 */
public class TabLayoutActivity extends BaseActivity {
    private Tab1LayoutBinding binding;
    private FragmentTabAdapter adapter;
    public List<Fragment> fragments = new ArrayList<Fragment>();
    private List<String> titles = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_tablayout_1);

        titles.clear();
        titles.add("发现");
        titles.add("发现");
        titles.add("发现");

        initView();
        initListener();
    }

    private void initListener() {
        binding.tabViewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                binding.tabViewpager.resetHeight(position);//每次切换页面，都重新重置高度
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        binding.tabViewpager.resetHeight(0);
    }

    private void initView() {

        fragments.add(new TestFrament(binding.tabViewpager, 0));
        fragments.add(new TestFrament(binding.tabViewpager, 1));
        fragments.add(new TestFrament(binding.tabViewpager, 2));
        adapter = new FragmentTabAdapter(getSupportFragmentManager(), fragments, titles);
        binding.tabViewpager.setAdapter(adapter);

        binding.tabTablayout.setupWithViewPager(binding.tabViewpager);
        binding.tabTablayout.setTabMode(TabLayout.MODE_FIXED);

    }
}
